package dream.zyp.ballgame.activity;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;

import dream.zyp.ballgame.component.Baffle;
import dream.zyp.ballgame.component.Ball;
import dream.zyp.ballgame.component.GameBlock;
import dream.zyp.ballgame.component.GameBlockGroup;
import dream.zyp.ballgame.component.ViewInfo;
import dream.zyp.ballgame.control.BlockBumpControl;
import dream.zyp.ballgame.tools.TouchAction;

/**
 * Created by ZYP on 2017/3/5.
 */
public class DrawThread extends Thread {
    private float ballXSpeed = 1f, baffleSpeed = 5.0f, ballYSpeed = 1f;
    public int a = 10;
    private SurfaceHolder holder;
    private ViewInfo viewInfo;  //控件信息
    private Ball ball;  //小球信息
    private Baffle baffle;  //挡板信息
    private Paint bafflePaint;  //挡板画笔
    private Paint ballPaint;  //小球画笔
    private Paint backPaint;  //背景画笔
    private Paint blocksPaint;
    private int backColor = Color.WHITE;
    boolean flag = true;
    private boolean draw = false;
    private GameBlockGroup blockGroup;


    private Handler controlHandler = new Handler() {
        /**
         * Subclasses must implement this to receive messages.
         *
         * @param msg
         */
        @Override
        public void handleMessage(Message msg) {
            int action = msg.what;
            if (action == TouchAction.LEFT) {
                //baffle.x -= baffleSpeed;
                changeBaffleState(action);
            } else if (action == TouchAction.RIGHT) {
                //baffle.x += baffleSpeed;
                changeBaffleState(action);
            } else if (action == 1111) {
                /*if (draw)
                    draw = false;
                else
                    draw = true;*/
                if (ballXSpeed > 0) {
                    ballXSpeed += 5;
                } else {
                    ballXSpeed -= 5;
                }

                if (ballYSpeed > 0) {
                    ballYSpeed += 5;
                } else {
                    ballYSpeed -= 5;
                }
            }
        }
    };

    public DrawThread() {

    }

    public DrawThread(SurfaceHolder holder) {
        this.holder = holder;

    }

    public void setHolder(SurfaceHolder holder) {
        this.holder = holder;
    }

    public void setViewInfo(ViewInfo viewInfo) {
        this.viewInfo = viewInfo;
        init();
    }


    /**
     * 对视图中组件进行初始化
     */
    private void init() {
        ball = new Ball();
        ball.x = viewInfo.getWidth() / 2;
        ball.y = viewInfo.getHeight() / 2;
        ballPaint = new Paint();
        ballPaint.setColor(ball.getBallColor());
        ballPaint.setAntiAlias(true);  //设置抗锯齿

        baffle = new Baffle();
        baffle.x = viewInfo.getWidth() / 2;
        baffle.y = viewInfo.getHeight() - Baffle.DISTANCE_OF_BOTTOM;
        bafflePaint = new Paint();
        bafflePaint.setColor(baffle.getBaffleColor());
        bafflePaint.setAntiAlias(true);

        backPaint = new Paint();
        backPaint.setColor(backColor);
        backPaint.setAntiAlias(true);

        blockGroup = new GameBlockGroup(viewInfo.getWidth(),
                viewInfo.getHeight() / 2);
        Log.e("viewinfo", "init: " + viewInfo.getWidth() + viewInfo.getHeight() / 2);
        blocksPaint = new Paint();
        blocksPaint.setColor(Color.GRAY);


        draw = true;

    }

    /**
     * 绘制背景图
     */
    private void drawBackground(Canvas canvas) {
        canvas.drawColor(backColor);
    }

    private void initBackground() {
        Canvas canvas = holder.lockCanvas();
        drawBackground(canvas);
        holder.unlockCanvasAndPost(canvas);
    }

    /**
     * 控制挡板的移动
     *
     * @param action
     */
    private void changeBaffleState(int action) {
        int flag = isBaffleBorder(2f);
        if (flag == 0) {
            if (action == TouchAction.LEFT) {
                baffle.x -= baffleSpeed;
            } else if (action == TouchAction.RIGHT) {
                baffle.x += baffleSpeed;
            }
        } else {
            if (flag == LEFT) {
                baffle.x += 2 * baffleSpeed;
            } else {
                baffle.x -= 2 * baffleSpeed;
            }
        }


    }

    private static final int LEFT = 1;
    private static final int RIGHT = 2;

    private int isBaffleBorder(float offset) {
        float left = baffle.x - baffle.getBaffleWidth() / 2 - offset;
        float right = baffle.x + baffle.getBaffleWidth() / 2 + offset;

        if (left <= 0) {
            return LEFT;
        }

        if (right > viewInfo.getWidth()) {
            return RIGHT;
        }

        return 0;

    }

    /**
     * 检查小球的边界。
     */
    private void checkBallBorder(float offset) {
        float right = ball.x + ball.getRadius() + offset + Math.abs(ballXSpeed);  // 小球右侧边界的坐标
        float left = ball.x - ball.getRadius() - offset - Math.abs(ballXSpeed);  //小球左侧边界的坐标
        float top = ball.y - ball.getRadius() - offset - Math.abs(ballYSpeed);
        float bottom = ball.y + ball.getRadius() + offset + Math.abs(ballYSpeed);
        // 检测左右边界
        if (right >= viewInfo.getWidth()) {
            //如果超过了右侧边界
            ballXSpeed = -Math.abs(ballXSpeed);
        }
        if (left <= 0) {
            ballXSpeed = Math.abs(ballXSpeed);
        }

        if (top <= 0) {
            ballYSpeed = Math.abs(ballYSpeed);
        }

        //如果在挡板位置，进行反弹
        if (bottom >= (baffle.y - baffle.getBaffleHeight()) &&
                ball.x >= (baffle.x - baffle.getBaffleWidth() / 2) &&
                ball.x <= (baffle.x + baffle.getBaffleWidth() / 2)) {
            ballYSpeed = -Math.abs(ballYSpeed);
        }

        BlockBumpControl control = blockGroup.changeBlockState(ball.x, ball.y);
        if (control.upOrDown) {
            ballYSpeed = Math.abs(ballYSpeed);
            if (control.leftOrRight != null) {
                if (ballXSpeed > 0) {
                    ballXSpeed = -Math.abs(ballXSpeed);
                } else {
                    ballXSpeed = Math.abs(ballXSpeed);
                }

            }
        }

    }


    public Handler getControlHandler() {
        return controlHandler;
    }

    public void drawBlocks(GameBlock[][] blocks, int rowNumber, int colNumber, Canvas canvas, Paint paint) {
        for (int i = 0; i < rowNumber; i++) {
            for (int j = 0; j < colNumber; j++) {
                canvas.drawRect(blocks[i][j].x,
                        blocks[i][j].y + GameBlockGroup.blockHeight,
                        blocks[i][j].x + GameBlockGroup.blockWidth,
                        blocks[i][j].y, paint);
            }
        }
    }

    @Override
    public void run() {
        initBackground();

        while (flag) {
            while (draw) {
                Canvas canvas = holder.lockCanvas();
                drawBackground(canvas);

                canvas.drawRect(baffle.x - (baffle.getBaffleWidth() / 2),
                        baffle.y - baffle.getBaffleHeight(),
                        baffle.x + (baffle.getBaffleWidth() / 2),
                        baffle.y, bafflePaint);
                blockGroup.drawBlocks(canvas, blocksPaint);
                /*GameBlock[][] blocks = blockGroup.getBlocks();
                int rowNumber = blockGroup.getRowNumber();
                int colNumber = blockGroup.getColNumber();

                for (int i = 0; i < rowNumber; i++){
                    for (int j = 0; j < colNumber; j++){
                        canvas.drawRect(blocks[i][j].x,
                                blocks[i][j].y + GameBlockGroup.blockHeight,
                                blocks[i][j].x + GameBlockGroup.blockWidth,
                                blocks[i][j].y,blocksPaint);
                    }
                }*/

                canvas.drawCircle(ball.x, ball.y, ball.getRadius(), ballPaint);

                checkBallBorder(0.5f);
                ball.x += ballXSpeed;
                ball.y += ballYSpeed;
                //changeBaffleState(TouchAction.LEFT);
                holder.unlockCanvasAndPost(canvas);
            }

        }
    }
}
